package com.singfan.script.importdata;

import com.singfan.common.util.EncryptionUtil;
import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.IShopAdditionalDao;
import com.singfan.core.dao.database.IShopDao;
import com.singfan.core.dao.database.IShopHairDao;
import com.singfan.core.dao.mongo.IShopLocationDao;
import com.singfan.core.dao.database.model.Shop;
import com.singfan.core.dao.database.model.ShopAdditional;
import com.singfan.core.dao.database.model.ShopHair;
import com.singfan.core.dao.database.model.constant.BusinessStatusType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Author: hansonghe
 * Date: 2015-08-19 17:39
 * Desc:
 */
@Component
public class ShopSheetParser extends SheetParser {
    @Autowired
    private IShopDao shopDao;
    @Autowired
    private IShopAdditionalDao shopAdditionalDao;
    @Autowired
    private IShopHairDao shopHairDao;

    @Autowired
    private IShopLocationDao shopLocationDao;

    @Override
    protected void insertData(XSSFRow row) {
        String defaultPassword = "123456";

        int colIndex = 0;
        Shop shop = new Shop();
        shop.setId((long) row.getCell(colIndex++).getNumericCellValue());
        shop.setRegisterPhone(row.getCell(colIndex++).getStringCellValue());
        shop.setMasterShopId((int) row.getCell(colIndex++).getNumericCellValue());
        shop.setMasterShopName(row.getCell(colIndex++).getStringCellValue());
        shop.setSlaveShopName(row.getCell(colIndex++).getStringCellValue());
        shop.setShopIconUrl(row.getCell(colIndex++).getStringCellValue());
        shop.setImgList(row.getCell(colIndex++).getStringCellValue());
        shop.setCompanyType((int) row.getCell(colIndex++).getNumericCellValue());
        shop.setDescription(row.getCell(colIndex++).getStringCellValue());
        shop.setAddress(row.getCell(colIndex++).getStringCellValue());
        shop.setLongitude(new BigDecimal(row.getCell(colIndex++).getNumericCellValue()));
        shop.setLatitude(new BigDecimal(row.getCell(colIndex++).getNumericCellValue()));
        shop.setCityId((int) row.getCell(colIndex++).getNumericCellValue());
        shop.setDistrictId((int) row.getCell(colIndex++).getNumericCellValue());
        shop.setBusinessCircleId((int) row.getCell(colIndex++).getNumericCellValue());
        shop.setContactNumber(row.getCell(colIndex++).getStringCellValue());
        shop.setHonor((int) row.getCell(colIndex++).getNumericCellValue());

        int orderCount = (int)row.getCell(colIndex++).getNumericCellValue();
        String hairStyleService = row.getCell(colIndex++).getStringCellValue();

        Date now = new Date();
        String passwordSalt = UUID.randomUUID().toString().substring(0, 7);
        String password = EncryptionUtil.md5(defaultPassword + "-" + passwordSalt);
        shop.setPassword(password);
        shop.setPasswordSalt(passwordSalt);
        shop.setContactNumber(shop.getRegisterPhone());
        shop.setStatus(BusinessStatusType.NOT_VERIFY);
        shop.setCreateTime(now);
        shop.setUpdateTime(now);

        shopDao.create(shop);

        List<String> serviceStrList = StringUtil.split(hairStyleService, ";");
        List<Integer> serviceValueList = new ArrayList<>(serviceStrList.size());
        for (String serviceStr : serviceStrList){
            serviceValueList.add(Integer.valueOf(serviceStr) - 20);
        }
        shopLocationDao.createShopLocation(shop.getMasterShopId(), shop.getId(), shop.getCityId(), shop.getBusinessCircleId(), serviceValueList, shop.getLongitude().doubleValue(), shop.getLatitude().doubleValue());

        ShopAdditional shopAdditional = new ShopAdditional();
        shopAdditional.setShopId(shop.getId());
        shopAdditional.setCompanyType(shop.getCompanyType());
        if (StringUtil.isNullOrEmpty(shop.getSlaveShopName())){
            shopAdditional.setCompanyName(shop.getMasterShopName());
        }
        else{
            shopAdditional.setCompanyName(shop.getMasterShopName() + shop.getSlaveShopName());
        }
        shopAdditional.setVerifyStatus(BusinessStatusType.NOT_VERIFY);
        shopAdditional.setCreateTime(now);
        shopAdditional.setUpdateTime(now);
        shopAdditionalDao.create(shopAdditional);

        ShopHair shopHair = new ShopHair();
        shopHair.setShopId(shop.getId());
        shopHair.setMasterShopId(shop.getMasterShopId());
        shopHair.setMasterShopName(shop.getMasterShopName());
        shopHair.setSlaveShopName(shop.getSlaveShopName());
        shopHair.setPreferredImg(shop.getShopIconUrl());
        shopHair.setImgList(shop.getImgList());
        shopHair.setDescription(shop.getDescription());
        shopHair.setAddress(shop.getAddress());
        shopHair.setLongitude(shop.getLongitude());
        shopHair.setLatitude(shop.getLatitude());
        shopHair.setCityId(shop.getCityId());
        shopHair.setDistrictId(shop.getDistrictId());
        shopHair.setBusinessCircleId(shop.getBusinessCircleId());
        shopHair.setStatus(BusinessStatusType.NOT_VERIFY);
        shopHair.setBarberCount(0);
        shopHair.setOrderCount(orderCount);
        shopHair.setHonor(shop.getHonor());
        shopHair.setCommentCount(orderCount);
        shopHair.setCommentScore(orderCount * shop.getHonor());

        String[] hairServiceIdArr = hairStyleService.split(";");
        for (String hairService : hairServiceIdArr){
            if (!StringUtil.isNullOrEmpty(hairService)){
                if (hairService.equals("21")){
                    shopHair.setHairCut(true);
                }
                else if(hairService.equals("22")){
                    shopHair.setHairPerm(true);
                }
                else if(hairService.equals("23")){
                    shopHair.setHairDye(true);
                }
                else if(hairService.equals("24")){
                    shopHair.setHairNurse(true);
                }
                else if(hairService.equals("25")){
                    shopHair.setHairModeling(true);
                }
            }
        }
        shopHair.setMan30(false);
        shopHair.setMan60(false);
        shopHair.setMan90(false);
        shopHair.setCreateTime(now);
        shopHairDao.create(shopHair);
    }
}
